// pages/workerDetail/workerDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    goodsDetail: '',
    skuDisplay: '',
    coupons: '',
    step: 1
  },

  prevImg(e) {
    let src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src,
      urls: this.data.goodsDetail.scrollImages_,
    })
  },

  prevHead(e) {
    let src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src,
      urls: [this.data.goodsDetail.listImage]
    })
  },

  navEvaList() {
    wx.$nav('/pages/evaList/evaList', {
      goodsId: this.data.goodsId
    })
  },

  skuChoose(e) {
    this.setData({
      skuDisplay: e.detail
    })
  },

  numChange(e) {
    this.setData({
      step: e.detail
    })
  },

  setNum(e) {
    if (this.data.goodsDetail.shopId == 0 || !this.data.goodsDetail.shopId)
      this.topStep.setNum(e.detail)
  },

  collec() {
    let that = this
    wx.$isLogin(() => {
      wx.$https('goods.col', {
        goodsId: this.data.goodsId
      }).then(res => {
        let detail = this.data.goodsDetail
        detail.isCol = res.result
        this.setData({
          goodsDetail: detail
        })
      })
    }, '/pages/workerDetail/workerDetail', {
      goodsId: that.data.goodsId
    })
  },

  navSer() {
    wx.$nav('/pages/service/service', {
      shopId: this.data.goodsDetail.shopId || 0
    })
  },

  getDesc() {
    wx.$https('goods.desc', {
      goodsId: this.data.goodsId
    }).then(res => {
      let goodsDetail = res
      goodsDetail.label ? (goodsDetail.labels = goodsDetail.label.split(',')) : (goodsDetail.labels = [])
      goodsDetail['scrollImages_'] = goodsDetail.scrollImages.split(',')
      // goodsDetail['contentHtml_'] = goodsDetail.contentHtml.split(',')
      this.setData({
        goodsDetail: goodsDetail
      })
      wx.setNavigationBarTitle({
        title: goodsDetail.workName || goodsDetail.title,
      })
      this.sku = this.selectComponent("#sku");
      this.topStep = this.selectComponent("#topStep");
      this.sku.init(this.data.goodsDetail, 1)
      this.getCouponInfo();
    })
  },

  getCouponInfo() {
    let goodsDetail = this.data.goodsDetail
    // if (!goodsDetail.shopId || goodsDetail.shopId == '0') return
    wx.$https('coupon.list', {
      start: 0,
      limit: 10,
      shopId: goodsDetail.shopId || 0
    }).then(res => {
      this.setData({
        coupons: res
      })
    })
  },

  navGetCoupon() {
    wx.$isLogin(() => {
      wx.$nav('/pages/getCoupon/getCoupon', {
        // list: JSON.stringify(this.data.coupons),
        shopId: this.data.goodsDetail.shopId
      })
    }, '/pages/workerDetail/workerDetail', {
      goodsId: that.data.goodsId
    })
  },

  skuShow() {
    wx.$isLogin(() => {
      this.sku.hideShow(this.data.step)
    }, '/pages/workerDetail/workerDetail', {
      goodsId: this.data.goodsId
    })
  },

  onLoad: function (options) {
    if (options.goodsId) {
      this.setData({
        goodsId: options.goodsId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDesc()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})